//
//  CustomSuggestionAlert.m
//  HuoBao
//
//  Created by 于会海 on 15/12/4.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "CustomSuggestionAlert.h"
#import "HBReSetTradpasswordCtrl.h"

@interface CustomSuggestionAlert ()<UITextFieldDelegate>
{
    UIView*    bgview;
    UIView*    dangban;
    UILabel*   alertLabel;
}
#define GetFinalResult(x,y,z,r) (IsIphone4?x:(IsIphone6P?r:(IsIphone5?y:z)))
#define ScaleForm5To6    1.172
//#define ScaleForm4To6    1.389
#define ScaleForm4To6    1.119
#define alertViewW       GetFinalResult(275/ScaleForm4To6,275/ScaleForm5To6,275,275*1.104)
#define alertViewH       GetFinalResult(183/ScaleForm4To6,183/ScaleForm5To6,183,183*1.104)
#define bgviewToDangbanW       GetFinalResult(45/ScaleForm4To6,45/ScaleForm5To6,45,45*1.104)
#define bgviewToDangbanH       GetFinalResult(192/ScaleForm4To6,192/ScaleForm5To6,192,192*1.104)
@end
@implementation CustomSuggestionAlert

- (instancetype)init{
    self = [super init];
    
    dangban = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    dangban.backgroundColor = [UIColor blackColor];
    dangban.alpha = 0.3;
    [self addSubview:dangban];
    
    bgview = [[UIView alloc]initWithFrame:CGRectMake(bgviewToDangbanW, bgviewToDangbanH, alertViewW, alertViewH)];
    bgview.centerX = SCREEN_WIDTH/2;
    bgview.centerY = SCREEN_HEIGHT/2 - 40;
    bgview.backgroundColor = [LCCommon getColor:@"3d3e41"];
    bgview.alpha = 0;
    bgview.layer.cornerRadius = 4;
    [self addSubview:bgview];
    
    UILabel* topTitle = [self LabelWithRect:CGRectMake(0, GetFinalResult(15, 15, 23, 23), alertViewW, 22) textColor:[LCCommon getColor:@"ffffff"] textstr:@"提 示" textfont:GetFinalResult(20, 20, 22, 22) textstyle:NSTextAlignmentCenter];
    [bgview addSubview:topTitle];
    
    alertLabel = [self LabelWithRect:CGRectMake(0, GetFinalResult(35, 40, 50, 50), alertViewW, 60) textColor:[LCCommon getColor:@"b1b1b1"] textstr:@"您输入密码的错误次数超过限制，\n请30分钟后再试，或者找回提现密码" textfont:GetFinalResult(13,13,13,13) textstyle:NSTextAlignmentCenter];
    [bgview addSubview:alertLabel];
    
    [self makeLinewithRect:CGRectMake(0, alertViewH-45, alertViewW, 1) Color:[LCCommon getColor:@"696969"] addview:bgview];
    [self makeLinewithRect:CGRectMake(alertViewW/2, alertViewH-45, 1, 45) Color:[LCCommon getColor:@"696969"] addview:bgview];

    
    UIButton*  leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.tag = 2500;
    [leftBtn.layer setMasksToBounds:YES];
    [leftBtn.layer setCornerRadius:4.0];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:GetFinalResult(13, 15, 15, 15)];
    [leftBtn setTitle:@"稍后再试" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[LCCommon getColor:@"ffffff"] forState:UIControlStateNormal];
    [leftBtn setFrame:CGRectMake(1, alertViewH -44, alertViewW/2 -1, 44)];
    [leftBtn setBackgroundColor:[UIColor clearColor] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setBackgroundColor:[LCCommon getColor:@"343538"] forState:UIControlStateHighlighted];
    [bgview addSubview:leftBtn];
    
    UIButton*  rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.tag = 2501;
    [rightBtn.layer setMasksToBounds:YES];
    [rightBtn.layer setCornerRadius:4.0];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:GetFinalResult(13, 15, 15, 15)];
    [rightBtn setTitle:@"找回密码" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[LCCommon getColor:@"ffffff"] forState:UIControlStateNormal];
    [rightBtn setFrame:CGRectMake(alertViewW/2 +1, alertViewH -44, alertViewW/2-1, 44)];
    [rightBtn setBackgroundColor:[UIColor clearColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setBackgroundColor:[LCCommon getColor:@"343538"] forState:UIControlStateHighlighted];
    [bgview addSubview:rightBtn];
    return self;
}

- (void)btnAction:(UIButton*)button{
    if (button.tag == 2500) {
        LLog(@"稍后再试");

        [UIView animateWithDuration:0.35 animations:^{
            self.alpha = 0;
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self removeFromSuperview];
        });


    }else if(button.tag == 2501){
        LLog(@"找回密码");
        if(self.buttonClicked){
            self.buttonClicked(button.tag);
             [self removeFromSuperview];
        }
    }
}

-(UILabel *)LabelWithRect:(CGRect)rect textColor:(UIColor *)color textstr:(NSString *)str textfont:(int)font textstyle:(NSTextAlignment)aligment{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:str];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    //设置行距
    [style setLineSpacing:GetFinalResult(7, 7, 10, 10)];
    [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [attStr length])];
    label.attributedText = attStr;
    label.textColor = color;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = aligment;
    label.numberOfLines = 0;
    return label;
}

-(void)makeLinewithRect:(CGRect)rect Color:(UIColor *)color addview:(UIView *)vi{
    UIView *line = [[UIView alloc]initWithFrame:rect];
    line.backgroundColor = color;
    [vi addSubview:line];
}


- (void)showTradPasswordAlertView{
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        bgview.backgroundColor = [LCCommon getColor:@"3d3e41"];
        bgview.alpha = 1;
    }completion:^(BOOL finished) {
        
    }];
}
@end
